/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_api_testing.Java_Util_Arrays.java]
 * Creation Date 	[15-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_api_testing;

import java.util.Arrays;

/**
 * java.util.Arrays
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_Util_Arrays {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		// 填充整个数组
		int[] array0 = new int[10];
		Arrays.fill(array0, 5);
		System.out.println("填充后: " + Arrays.toString(array0));

		// 填充数组的指定位置[3,8]
		Arrays.fill(array0, 3, 8, 10);
		System.out.println("填充指定位置: " + Arrays.toString(array0));

		// 数组的排序
		int[] array1 = { 7, 8, 4, 5, 1, 12, 9 };
		System.out.println("排序前\t" + Arrays.toString(array1));
		Arrays.sort(array1);
		System.out.println("排序后\t" + Arrays.toString(array1));

		// 数组内容的比较
		char[] array2 = { 'a', 'b', 'c' };
		char[] array3 = { 'a', 'b', 'c' };
		System.out.println(Arrays.equals(array2, array3));
		System.out.println("Hash code of array2 = " + Arrays.hashCode(array2));
		System.out.println("Hash code of array3 = " + Arrays.hashCode(array3));
		// 对于char类型数组可以转换为string进行比较
		String str1 = new String(array2);
		String str2 = new String(array3);
		System.out.println(str1.equals(str2));

		/*
		 * 使用二分法在数组中查找指定元素, 如果找到返回该元素的下标, 如果不存在则返回负数
		 * 
		 * 使用二分法查找的数组必须经过排序
		 */
		System.out.println(Arrays.binarySearch(array1, 4));

		/*
		 * 数组拷贝
		 * 
		 * arraycopy(源数组, 源数组的起始位置, 目标数组, 目标数组的起始位置, 拷贝几个元素);
		 */
		int[] array4 = { 1, 2, 3, 4, 5 };
		int[] array5 = new int[5];
		System.arraycopy(array4, 1, array5, 0, 3);
		System.out.println(Arrays.toString(array5));

		/*
		 * JDK6.0数组拷贝
		 */
		int[] array6 = new int[7];
		array6 = Arrays.copyOf(array4, array4.length);// array6拷贝后长度为5
		System.out.println(Arrays.toString(array6));

		// 二维数组
		int[][] array7 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		System.out.println(Arrays.deepToString(array7));

		// 拷贝数组
		int[] array8 = { 1, 2, 3, 4, 5, 6 };
		int[] array9 = array8.clone();
		array9[3] = 0;
		System.out.println(Arrays.toString(array8));
		System.out.println(Arrays.toString(array9));

	}
}
